//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.imagemanager.util;

import android.util.Log;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;

public final class DiskCache {
    private static final String TAG = "DiskCache";
    private static final int CHUNK_SIZE = 1048576;
    private static final int INDEX_HEADER_MAGIC = 51966;
    private static final int INDEX_HEADER_VERSION = 2;
    private static final String INDEX_FILE_NAME = "index";
    private static final String CHUNK_FILE_PREFIX = "chunk_";
    private final String mCacheDirectoryPath;
    private LongSparseArray<DiskCache.Record> mIndexMap;
    private final LongSparseArray<RandomAccessFile> mChunkFiles = new LongSparseArray();
    private int mTailChunk = 0;
    private int mNumInsertions = 0;

    public DiskCache(String cacheDirectoryName) {
        String cacheDirectoryPath = CacheService.getCachePath(cacheDirectoryName);
        File cacheDirectory = new File(cacheDirectoryPath);
        if(!cacheDirectory.isDirectory() && !cacheDirectory.mkdirs()) {
            Log.e("DiskCache", "Unable to create cache directory " + cacheDirectoryPath);
        }

        this.mCacheDirectoryPath = cacheDirectoryPath;
        this.loadIndex();
    }

    public void finalize() {
        this.shutdown();
    }

    public byte[] get(long key, long timestamp) {
        DiskCache.Record record = null;
        LongSparseArray var6 = this.mIndexMap;
        synchronized(this.mIndexMap) {
            record = (DiskCache.Record)this.mIndexMap.get(key);
        }

        if(record != null) {
            if(record.timestamp != timestamp) {
                return null;
            }

            try {
                RandomAccessFile chunkFile = this.getChunkFile(record.chunk);
                if(chunkFile != null) {
                    byte[] data = new byte[record.size];
                    chunkFile.seek((long)record.offset);
                    chunkFile.readFully(data);
                    return data;
                }
            } catch (Exception var8) {
                Log.e("DiskCache", "Unable to read from chunk file");
            }
        }

        return null;
    }

    public boolean isDataAvailable(long key, long timestamp) {
        DiskCache.Record record = null;
        LongSparseArray var6 = this.mIndexMap;
        synchronized(this.mIndexMap) {
            record = (DiskCache.Record)this.mIndexMap.get(key);
        }

        return record == null?false:(record.timestamp != timestamp?false:record.size != 0);
    }

    public void put(long key, byte[] data, long timestamp) {
        DiskCache.Record record = null;
        LongSparseArray var7 = this.mIndexMap;
        synchronized(this.mIndexMap) {
            record = (DiskCache.Record)this.mIndexMap.get(key);
        }

        int currentChunk;
        RandomAccessFile chunkFile;
        if(record != null && data.length <= record.sizeOnDisk) {
            currentChunk = record.chunk;

            try {
                chunkFile = this.getChunkFile(record.chunk);
                if(chunkFile != null) {
                    chunkFile.seek((long)record.offset);
                    chunkFile.write(data);
                    LongSparseArray var17 = this.mIndexMap;
                    synchronized(this.mIndexMap) {
                        this.mIndexMap.put(key, new DiskCache.Record(currentChunk, record.offset, data.length, record.sizeOnDisk, timestamp));
                        return;
                    }
                }
            } catch (Exception var14) {
                Log.e("DiskCache", "Unable to read from chunk file");
            }
        }

        currentChunk = this.mTailChunk;
        chunkFile = this.getChunkFile(currentChunk);
        if(chunkFile != null) {
            try {
                int offset = (int)chunkFile.length();
                chunkFile.seek((long)offset);
                chunkFile.write(data);
                LongSparseArray var10 = this.mIndexMap;
                synchronized(this.mIndexMap) {
                    this.mIndexMap.put(key, new DiskCache.Record(currentChunk, offset, data.length, data.length, timestamp));
                }

                if(offset + data.length > 1048576) {
                    ++this.mTailChunk;
                }

                if(++this.mNumInsertions == 64) {
                    this.flush();
                }
            } catch (IOException var12) {
                Log.e("DiskCache", "Unable to write new entry to chunk file");
            }
        } else {
            Log.e("DiskCache", "getChunkFile() returned null");
        }

    }

    public void delete(long key) {
        LongSparseArray var3 = this.mIndexMap;
        synchronized(this.mIndexMap) {
            this.mIndexMap.remove(key);
        }
    }

    public void deleteAll() {
        this.shutdown();
        File cacheDirectory = new File(this.mCacheDirectoryPath);
        String[] cacheFiles = cacheDirectory.list();
        if(cacheFiles != null) {
            String[] var6 = cacheFiles;
            int var5 = cacheFiles.length;

            for(int var4 = 0; var4 < var5; ++var4) {
                String cacheFile = var6[var4];
                (new File(cacheDirectory, cacheFile)).delete();
            }

        }
    }

    public void flush() {
        if(this.mNumInsertions != 0) {
            this.mNumInsertions = 0;
            this.writeIndex();
        }

    }

    public void close() {
        this.writeIndex();
        this.shutdown();
    }

    private void shutdown() {
        LongSparseArray var1 = this.mChunkFiles;
        synchronized(this.mChunkFiles) {
            int i = 0;
            int size = this.mChunkFiles.size();

            while(true) {
                if(i >= size) {
                    this.mChunkFiles.clear();
                    break;
                }

                try {
                    ((RandomAccessFile)this.mChunkFiles.valueAt(i)).close();
                } catch (Exception var6) {
                    Log.e("DiskCache", "Unable to close chunk file");
                }

                ++i;
            }
        }

        if(this.mIndexMap != null) {
            var1 = this.mIndexMap;
            synchronized(this.mIndexMap) {
                if(this.mIndexMap != null) {
                    this.mIndexMap.clear();
                }
            }
        }

    }

    private String getIndexFilePath() {
        return this.mCacheDirectoryPath + "index";
    }

    private void loadIndex() {
        String indexFilePath = this.getIndexFilePath();

        try {
            FileInputStream fileInput = new FileInputStream(indexFilePath);
            BufferedInputStream bufferedInput = new BufferedInputStream(fileInput, 1024);
            DataInputStream dataInput = new DataInputStream(bufferedInput);
            int magic = dataInput.readInt();
            int version = dataInput.readInt();
            boolean valid = true;
            if(magic != '쫾') {
                Log.e("DiskCache", "Index file appears to be corrupt (" + magic + " != " + '쫾' + "), " + indexFilePath);
                valid = false;
            }

            if(valid && version != 2) {
                Log.e("DiskCache", "Index file version " + version + " not supported");
                valid = false;
            }

            if(valid) {
                this.mTailChunk = dataInput.readShort();
            }

            if(valid) {
                int numEntries = dataInput.readInt();
                this.mIndexMap = new LongSparseArray(numEntries);
                LongSparseArray var9 = this.mIndexMap;
                synchronized(this.mIndexMap) {
                    for(int i = 0; i < numEntries; ++i) {
                        long key = dataInput.readLong();
                        int chunk = dataInput.readShort();
                        int offset = dataInput.readInt();
                        int size = dataInput.readInt();
                        int sizeOnDisk = dataInput.readInt();
                        long timestamp = dataInput.readLong();
                        this.mIndexMap.append(key, new DiskCache.Record(chunk, offset, size, sizeOnDisk, timestamp));
                    }
                }
            }

            dataInput.close();
            if(!valid) {
                this.deleteAll();
            }
        } catch (FileNotFoundException var25) {
            ;
        } catch (IOException var26) {
            Log.e("DiskCache", "Unable to read the index file " + indexFilePath);
        } finally {
            if(this.mIndexMap == null) {
                this.mIndexMap = new LongSparseArray();
            }

        }

    }

    private void writeIndex() {
        String indexFilePath = this.getIndexFilePath();

        try {
            File tempFile = File.createTempFile("DiskCacheIndex", (String)null);
            FileOutputStream fileOutput = new FileOutputStream(tempFile);
            BufferedOutputStream bufferedOutput = new BufferedOutputStream(fileOutput, 1024);
            DataOutputStream dataOutput = new DataOutputStream(bufferedOutput);
            int numRecords = this.mIndexMap.size();
            dataOutput.writeInt('쫾');
            dataOutput.writeInt(2);
            dataOutput.writeShort(this.mTailChunk);
            dataOutput.writeInt(numRecords);

            for(int i = 0; i < numRecords; ++i) {
                long key = this.mIndexMap.keyAt(i);
                DiskCache.Record record = (DiskCache.Record)this.mIndexMap.valueAt(i);
                dataOutput.writeLong(key);
                dataOutput.writeShort(record.chunk);
                dataOutput.writeInt(record.offset);
                dataOutput.writeInt(record.size);
                dataOutput.writeInt(record.sizeOnDisk);
                dataOutput.writeLong(record.timestamp);
            }

            dataOutput.close();
            Log.d("DiskCache", "Wrote index with " + numRecords + " records.");
            tempFile.renameTo(new File(indexFilePath));
        } catch (IOException var11) {
            Log.e("DiskCache", "Unable to write the index file " + indexFilePath);
        }catch (Exception e){
            Log.e("DiskCache", "Unable to write the index file " + e!=null?e.getMessage():"rename error");
        }

    }

    private RandomAccessFile getChunkFile(int chunk) {
        RandomAccessFile chunkFile = null;
        LongSparseArray var3 = this.mChunkFiles;
        synchronized(this.mChunkFiles) {
            chunkFile = (RandomAccessFile)this.mChunkFiles.get((long)chunk);
        }

        if(chunkFile == null) {
            String chunkFilePath = this.mCacheDirectoryPath + "chunk_" + chunk;

            try {
                chunkFile = new RandomAccessFile(chunkFilePath, "rw");
            } catch (FileNotFoundException var6) {
                Log.e("DiskCache", "Unable to create or open the chunk file " + chunkFilePath);
            }

            LongSparseArray var4 = this.mChunkFiles;
            synchronized(this.mChunkFiles) {
                this.mChunkFiles.put((long)chunk, chunkFile);
            }
        }

        return chunkFile;
    }

    private static final class Record {
        public final long timestamp;
        public final int chunk;
        public final int offset;
        public final int size;
        public final int sizeOnDisk;

        public Record(int chunk, int offset, int size, int sizeOnDisk, long timestamp) {
            this.chunk = chunk;
            this.offset = offset;
            this.size = size;
            this.timestamp = timestamp;
            this.sizeOnDisk = sizeOnDisk;
        }
    }
}
